#include <allegro.h>
#include <stdio.h>
#define NB_IMAGES 2



typedef struct {
BITMAP * img_right[NB_IMAGES];
BITMAP * img_left[NB_IMAGES];
BITMAP * bg;
int x, y;
int depl;
int direction; // 0:right 1:left
int vie;
}PERSO;

typedef struct {
BITMAP * img_balle;
int x, y;
int direction; // 0:right 1:left
}balle;


int main() {

char buf[50];
BITMAP *Buffer;
BITMAP *cou;
BITMAP *cou1;
BITMAP *game;

balle bl ;
balle bl1 ;
balle bl2 ;
balle bl3 ;


/*int song ;
song=chargerSon("song.wav");
voice_start(song);
voice_set_playmode(song,PLAYMODE_LOOP);
*/

PERSO perso;
PERSO perso1;
int i, compteur1,compteur2,m,f,t,u;


/* General initialization function */
allegro_init();
if (install_keyboard() == -1) {
allegro_message("Error! %s", allegro_error);
return -1;
}
set_color_depth(16);
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 950,560, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); /* here’s the command for text mode */
allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
return 1;
}
Buffer = create_bitmap(SCREEN_W, SCREEN_H);
//loading images

//loading perso
for (i = 0; i < NB_IMAGES; i++) {
sprintf(buf, "/home/l3ou/Bureau/prog2/abouright%d.bmp", i);
perso.img_right[i] = load_bitmap(buf, NULL);
sprintf(buf, "/home/l3ou/Bureau/prog2/abouleft%d.bmp", i);
perso.img_left[i] = load_bitmap(buf, NULL);
}
for (i = 0; i < NB_IMAGES; i++) {
sprintf(buf, "/home/l3ou/Bureau/prog2/morright%d.bmp", i);
perso1.img_right[i] = load_bitmap(buf, NULL);
sprintf(buf, "/home/l3ou/Bureau/prog2/morleft%d.bmp", i);
perso1.img_left[i] = load_bitmap(buf, NULL);
}

//init position & direction
perso.bg=load_bitmap("/home/l3ou/Bureau/prog2/Untitled-0.bmp", NULL);
cou=load_bitmap("/home/l3ou/Bureau/prog2/coeur4.bmp",NULL);
game=load_bitmap("/home/l3ou/Bureau/prog2/game_over.bmp",NULL);
cou1=load_bitmap("/home/l3ou/Bureau/prog2/coeur5.bmp",NULL);



bl.img_balle=load_bitmap("/home/l3ou/Bureau/prog2/bottle.bmp",NULL);
bl1.img_balle=load_bitmap("/home/l3ou/Bureau/prog2/shoe.bmp",NULL);
bl2.img_balle=load_bitmap("/home/l3ou/Bureau/prog2/knife.bmp",NULL);
bl3.img_balle=load_bitmap("/home/l3ou/Bureau/prog2/poele.bmp",NULL); 

compteur1 = 0;
perso.direction = 0;
perso.x = (SCREEN_W / 2)+100;
perso.y = (SCREEN_H / 2)+160;
perso.depl=1;
perso.vie=100;
compteur2 = 0;
perso1.direction = 0;
perso1.x = (SCREEN_W / 2)-100;
perso1.y = (SCREEN_H / 2)+160;
perso1.depl=1;
perso1.vie=100;

m=0;
		


while (!key[KEY_ENTER] ) {		
			 					
                               
                         if (key[KEY_ENTER]) {              
				SAMPLE *sound;

				if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
				allegro_message("Sound Error: %s", allegro_error);
				return 1; }

				set_volume(255, 255); //digital and music to max levels

				sound = load_sample("sifflet.wav");

				play_sample(sound, 255, 128, 1000, 0);

				} }

		

while (!key[KEY_ESC]/* && perso1.vie==100 && perso.vie==100*/) {
//vider le buffer
clear_bitmap(Buffer);
draw_sprite(Buffer, perso.bg, 0, 0);

if(perso.y != (SCREEN_H / 2)+160)
(perso.y)+=70;

if (key[KEY_UP] && (perso.x < SCREEN_W - perso.img_right[0]->w)) {
compteur1++;

(perso.y)-=70;
}
				


if (key[KEY_RIGHT] && (perso.x < SCREEN_W - perso.img_right[0]->w)) {
compteur1++;

(perso.x)+=perso.depl;
perso.direction = 0;
}

                               if (key[KEY_RIGHT]) {
				SAMPLE *sound;

				if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
				allegro_message("Sound Error: %s", allegro_error);
				return 1; }

				set_volume(255, 255); //digital and music to max levels

				sound = load_sample("path.wav");

				play_sample(sound, 255, 128, 1000, 0);

				}


if (key[KEY_LEFT] && (perso.x > 0 + perso.img_left[0]->w)) {
compteur1++;

(perso.x)-=perso.depl;
perso.direction = 1;
}
                              if (key[KEY_LEFT]) {
				SAMPLE *sound;

				if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
				allegro_message("Sound Error: %s", allegro_error);
				return 1; }

				set_volume(255, 255); //digital and music to max levels

				sound = load_sample("path.wav");

				play_sample(sound, 255, 128, 1000, 0);

				}



if (key[KEY_L]) { //shoot ball
m=1;
bl.x=(perso.x)+5;
bl.y=(perso.y)+20;
bl.direction=perso.direction;
}

if(m==1 && bl.direction==1){

bl.x=bl.x-5;
draw_sprite(Buffer, bl.img_balle,bl.x, bl.y);
} 


     //collision
       if (bl.x <= (perso1.x)+35 && (bl.x+20) >perso1.x && bl.y<(perso1.y+50) && bl.y>perso1.y ){m=0;
perso1.vie=0;}
if(perso1.vie==100){draw_sprite(Buffer,cou1,50,50);}
if(perso1.vie==0){draw_sprite(Buffer,cou,50,50);
draw_sprite(Buffer,game,250,270);} //game over


if(m==1 && bl.direction==0){
bl.x=bl.x+5;
draw_sprite(Buffer, bl.img_balle,bl.x, bl.y);
}

 
				if (key[KEY_L]) {
				SAMPLE *sound;

				if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
				allegro_message("Sound Error: %s", allegro_error);
				return 1; }

				set_volume(255, 255); //digital and music to max levels

				sound = load_sample("glass.wav");

				play_sample(sound, 255, 128, 1000, 0);

				}


if (key[KEY_K]) { //shoot ball
u=1;
bl2.x=(perso.x)+5;
bl2.y=(perso.y)+20;
bl2.direction=perso.direction;
}

if(u==1 && bl2.direction==1){

bl2.x=bl2.x-5;
draw_sprite(Buffer, bl2.img_balle,bl2.x, bl2.y);
} 


     //collision
       if (bl2.x <= (perso1.x)+35 && (bl2.x+20) >perso1.x && bl2.y<(perso1.y+50) && bl2.y>perso1.y ){u=0;
perso1.vie=0;}
if(perso1.vie==100){draw_sprite(Buffer,cou1,50,50);}
if(perso1.vie==0){draw_sprite(Buffer,cou,50,50);
draw_sprite(Buffer,game,250,270);} //game over


if(u==1 && bl2.direction==0){
bl2.x=bl2.x+5;
draw_sprite(Buffer, bl2.img_balle,bl2.x, bl2.y);
}

                                if (key[KEY_K]) {
                              SAMPLE *sound;

				if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
				allegro_message("Sound Error: %s", allegro_error);
				return 1; }

				set_volume(255, 255); //digital and music to max levels

				sound = load_sample("sword.wav");

				play_sample(sound, 255, 128, 1000, 0);

				}


//usleep(100000);

if (perso.direction == 0)
draw_sprite(Buffer, perso.img_right[compteur1 % NB_IMAGES ],perso.x, perso.y);
else
draw_sprite(Buffer, perso.img_left[compteur1 % NB_IMAGES ],perso.x, perso.y);



//blit(Buffer , screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
if (compteur1 == 1000)
compteur1 = 0;
if(perso1.y != (SCREEN_H / 2)+160)
(perso1.y)+=70;

if (key[KEY_D] && (perso1.x < SCREEN_W - perso1.img_right[0]->w)) {
compteur2++;

(perso1.x)+=perso1.depl;
perso1.direction = 0;
}
                              if (key[KEY_D]) {
				SAMPLE *sound;

				if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
				allegro_message("Sound Error: %s", allegro_error);
				return 1; }

				set_volume(255, 255); //digital and music to max levels

				sound = load_sample("path.wav");

				play_sample(sound, 255, 128, 1000, 0);

				}

if (key[KEY_Z] && (perso1.x < SCREEN_W - perso1.img_right[0]->w)) {
compteur2++;

(perso1.y)-=70;
}

                            



if (key[KEY_Q] && (perso1.x > 0 + perso1.img_left[0]->w)) {
compteur2++;

(perso1.x)-=perso1.depl;
perso1.direction = 1;
}
                               if (key[KEY_Q]) {
				SAMPLE *sound;

				if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
				allegro_message("Sound Error: %s", allegro_error);
				return 1; }

				set_volume(255, 255); //digital and music to max levels

				sound = load_sample("path.wav");

				play_sample(sound, 255, 128, 1000, 0);

				}


if (key[KEY_F]) {//shoot ball
f=1;
bl1.x=(perso1.x)+15;
bl1.y=(perso1.y)+15;
bl1.direction=perso1.direction;
}

if(f==1 && bl1.direction==1){
bl1.x=bl1.x-5;

draw_sprite(Buffer, bl1.img_balle,bl1.x, bl1.y);
} if (bl1.x <= (perso.x)+35 && (bl1.x+20) >perso.x && bl1.y<(perso.y+50) && bl1.y>perso.y ){f=0;perso1.vie=0;}


if(perso.vie==100){draw_sprite(Buffer,cou1,800,50);}
if(perso.vie==0){draw_sprite(Buffer,cou,800,50);
draw_sprite(Buffer,game,200,200);}

if(f==1 && bl1.direction==0){
bl1.x=bl1.x+5;
draw_sprite(Buffer, bl1.img_balle,bl1.x, bl1.y);

}


				if (key[KEY_F]) {
				SAMPLE *sound;

				if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
				allegro_message("Sound Error: %s", allegro_error);
				return 1; }

				set_volume(255, 255); //digital and music to max levels

				sound = load_sample("song.wav");

				play_sample(sound, 255, 128, 1000, 0);

				}


if (key[KEY_G]) {
			t=1;	
			bl3.x=(perso1.x)+15;
			bl3.y=(perso1.y)+15;
			bl3.direction=perso1.direction;
		
                }		


if(t==1 && bl3.direction==1){

                        bl3.x=bl3.x-5;
			draw_sprite(Buffer, bl3.img_balle,bl3.x, bl3.y);
		         draw_sprite(Buffer, bl3.img_balle,bl3.x, bl3.y);
}  
                if (bl3.x <= (perso.x)+35 && (bl3.x+20) >perso.x && bl3.y<(perso.y+50) && bl3.y>perso.y ){t=0;perso1.vie=0;}


if(perso.vie==100){draw_sprite(Buffer,cou1,800,50);}
if(perso.vie==0){draw_sprite(Buffer,cou,800,50);
draw_sprite(Buffer,game,200,200);}
		


             if(t==1 && bl3.direction==0){
			bl3.x=bl3.x+5;
			draw_sprite(Buffer, bl3.img_balle,bl3.x, bl3.y);
}







				if (key [KEY_G]) { 


				/*int chargerSon(char chemin[])
				{
				SAMPLE *samp;
				int v;
				samp=load_sample(chemin);
				if((v=allocate_voice(samp))==-1)
				{
				destroy_sample(samp);
				return 0;
				}
				return v;
				}

				*/
				SAMPLE *sound;

				if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
				allegro_message("Sound Error: %s", allegro_error);
				return 1; }

				set_volume(255, 255); //digital and music to max levels

				sound = load_sample("fer.wav");

				play_sample(sound, 255, 128, 1000, 0);

				}


if (perso1.direction == 0)
draw_sprite(Buffer, perso1.img_right[compteur2 % NB_IMAGES ],perso1.x, perso1.y);
else
draw_sprite(Buffer, perso1.img_left[compteur2 % NB_IMAGES ],perso1.x, perso1.y);
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);





//usleep(10000);
if (compteur2 == 1000){
compteur2 = 0;
}

}


//destroy sprites
for (i = 0; i < NB_IMAGES; i++) {
destroy_bitmap(perso1.img_right[i]);
destroy_bitmap(perso1.img_left[i]);
}
destroy_bitmap(Buffer);
return 0;

} 
END_OF_MAIN();
